package com.aistar.service.user.impl;

import com.aistar.mapper.UserMapper;
import com.aistar.model.entity.User;
import com.aistar.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BCryptPasswordEncoder encoder;


    @Override
    public User login(String email) {
        return userMapper.findByEmail(email);
    }

    @Override
    public User loginByPhone(String phone) {
        User user = userMapper.findByPhone(phone);
        return user;
    }

    @Override
    public User findByEmail(String email) {
        return userMapper.findByEmail(email);
    }

    @Override
    public Integer insterUser(User user) {
        String encoderPwd = encoder.encode(user.getPassword());
        user.setPassword(encoderPwd);
        return userMapper.insterUser(user);
    }
}
